// pages/music-player/index.js
// import { getSongDetail, getSongLyric } from '../../service/api_player'
// import { parseLyric } from '../../utils/parse-lyric'

import { audioContext, playerStore } from '../../store/index'

const playModeNames = ['order', 'repeat', 'random']

Page({
	data: {
		id: 0,
		currentSong: {},
		durationTime: 0,
		lyricInfos: [],

		currentTime: 0,
		currentLyricIndex: -1,
		currentLyricText: '',

		isMusicLyric: true,
		currentPage: 0,
		contentHeight: 0,
		sliderValue: 0,
		isSliderChanging: false,
		lyricScrollTop: 0,

		playModeIndex: 0,
		playModeName: 'order',
		isPlaying: false
	},

	onLoad(options) {
		// 1.获取传入的id
		const id = options.id
		this.setData({ id })

		// 2.根据id获取歌曲信息, 初始化音乐
		this.setupPlayerStoreListener()

		// 3.动态计算内容高度
		const globalData = getApp().globalData
		const screenHeight = globalData.screenHeight
		const statusBarHeight = globalData.statusBarHeight
		const navBarHeight = globalData.navBarHeight
		const deviceRadio = globalData.deviceRadio
		const contentHeight = screenHeight - statusBarHeight - navBarHeight
		this.setData({ contentHeight, isMusicLyric: deviceRadio >= 2 })
	},

	// 返回事件
	handleBackBtn() {
		wx.navigateBack()
	},

	// 页面切换
	handleSwiperChange(event) {
		const currentPage = event.detail.current
		this.setData({ currentPage })
	},

	// 进度条控制
	handleSliderChange(event) {
		const sliderValue = event.detail.value

		const currentTime = (this.data.durationTime * sliderValue) / 100
		// audioContext.pause()
		audioContext.seek(currentTime / 1000)

		this.setData({ sliderValue, isSliderChanging: false })
	},
	handleSliderChanging(event) {
		const value = event.detail.value
		const currentTime = (this.data.durationTime * value) / 100
		this.setData({ isSliderChanging: true, currentTime })
	},
	// 修改播放模式
	changePlayMode() {
		let playModeIndex = this.data.playModeIndex + 1
		if (playModeIndex === 3) playModeIndex = 0
		playerStore.setState('playModeIndex', playModeIndex)
	},
	// 修改播放状态
	changePlayStatus() {
		playerStore.dispatch('changeMusicPlayStatusAction', !this.data.isPlaying)
	},
	// 上一首
	handlePrevBtn() {
		playerStore.dispatch('changeNewMusicAction', false)
	},
	// 下一首
	handleNextBtn() {
		playerStore.dispatch('changeNewMusicAction', true)
	},

	// 从store中获取歌曲信息
	setupPlayerStoreListener() {
		playerStore.onStates(['currentSong', 'durationTime', 'lyricInfos'], ({ currentSong, durationTime, lyricInfos }) => {
			if (currentSong) this.setData({ currentSong })
			if (durationTime) this.setData({ durationTime })
			if (lyricInfos) this.setData({ lyricInfos })
		})

		playerStore.onStates(['currentTime', 'currentLyricIndex', 'currentLyricText'], ({ currentTime, currentLyricIndex, currentLyricText }) => {
			if (currentTime && !this.data.isSliderChanging) {
				this.setData({ currentTime })
				this.setData({ sliderValue: (currentTime / this.data.durationTime) * 100 })
			}
			if (currentLyricIndex) {
				this.setData({ currentLyricIndex, lyricScrollTop: currentLyricIndex * 35 })
			}
			if (currentLyricText) {
				this.setData({ currentLyricText })
			}
		})

		playerStore.onStates(['playModeIndex', 'isPlaying'], ({ playModeIndex, isPlaying }) => {
			if (playModeIndex !== undefined) {
				this.setData({ playModeIndex, playModeName: playModeNames[playModeIndex] })
			}
			if (isPlaying !== undefined) {
				this.setData({ isPlaying })
			}
		})
	},
	onUnload() {}
})
